﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Nizm0.QOT.Library.Objects
{
    [Serializable]
    public class Variable : Library.Objects.IVariable
    {

        protected string _name;
        protected string _datatype;
        protected bool _output = false;
        //private string _defaultvalue;
        //private bool _output = false;
        protected IProcedure _parent;

        public static string[] DataTypeTab = { "bigint", "int", "smallint", "tinyint", "bit", "decimal", "numeric", "money", "smallmoney", "float", "real", "datetime", "smalldatetime", "char", "varchar", "text", "nchar", "nvarchar", "ntext", "binary", "varbinary", "image", "cursor", "sql_variant", "table", "timestamp", "uniqueidentifier" };
        //public enum EnumDatatype { bigint, int, "smallint", "tinyint", "bit", "decimal", "numeric", "money", "smallmoney", "float", "real", "datetime", "smalldatetime", "char", "varchar", "text", "nchar", "nvarchar", "ntext", "binary", "varbinary", "image", "cursor", "sql_variant", "table", "timestamp", "uniqueidentifier" };

        [CategoryAttribute("Misc")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        [CategoryAttribute("Misc")]
        public string DataType
        {
            get { return _datatype; }
            set { _datatype = value; }
        }
        public bool Output
        {
            get { return _output; }
        }
        //public string DefaultValue
        //{
        //    get { return _defaultvalue; }
        //}
        [Browsable(false)]
        public IProcedure Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }
        //public bool Output
        //{
        //    get { return _output; }
        //}

        public Variable(string name, string datatype, Procedure parent)
        {
            _name = name;
            _datatype = datatype;
            _parent = parent;
        }

        public Variable(Variable var)
        {
            _name = var.Name;
            _datatype = var.DataType;
            //_output = var.Output;
            //_defaultvalue = var.DefaultValue;
            _parent = var.Parent;
        }
        public Variable() { }

        public override string ToString()
        {
            return this._name;
        }
    }

    [Serializable]
    public class VariableFromQuery : Variable
    {
        private string _value;

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public VariableFromQuery()
            : base()
        {
        }

        public VariableFromQuery(string name)
            : base()
        {
            _name = name;
        }

        public VariableFromQuery(string name, string datatype, bool output)
        {
            _name = name;
            _datatype = datatype;
            _output = output;
        }

        public VariableFromQuery(string value, Variable var)
            : base(var)
        {
            _value = value;
        }

        public override string ToString()
        {
            return _value;
        }
    }

    [Serializable]
    public class VariableInTheProcedure : Variable
    {
        //private string _name;
        //private string _datatype;
        private string _defaultvalue;
        private string _comment = "";
        //private ProcedureSQLInfo _parent;

        //string[] DataTypeTab = { "bigint", "int", "smallint", "tinyint", "bit", "decimal", "numeric", "money", "smallmoney", "float", "real", "datetime", "smalldatetime", "char", "varchar", "text", "nchar", "nvarchar", "ntext", "binary", "varbinary", "image", "cursor", "sql_variant", "table", "timestamp", "uniqueidentifier" };

        //public string Name
        //{
        //    get { return _name; }
        //}
        //public string DataType
        //{
        //    get { return _datatype; }
        //}
        public string DefaultValue
        {
            get { return _defaultvalue; }
        }
        //public ProcedureSQLInfo Parent
        //{
        //    get { return _parent; }
        //    set { _parent = value; }
        //}
        public string Comment
        {
            get { return _comment; }
            set { _comment = value; }
        }

        public VariableInTheProcedure(Variable var, string data)
            : base(var)
        {
            GetInfo(data);
        }

        //public VariableInTheProcedure(string data)
        //{
        //    GetInfo(data);
        //}

        public VariableInTheProcedure(VariableInTheProcedure var)
            : base((Variable)var)
        {
            //base.Name = var.Name;
            //_datatype = var.DataType;
            _output = var.Output;
            _defaultvalue = var.DefaultValue;
        }

        protected VariableInTheProcedure()
        { }

        public void GetInfo(string data)
        {
            string[] tabstr;
            if (data.Contains('=') || data.Contains(" as ") || data.Contains(" AS "))
            {
                string[] ch = { " ", "=", " as ", " AS " };
                if (data.Contains("--"))
                {
                    string[] tmpdata = data.Split(new string[] { "--" }, StringSplitOptions.RemoveEmptyEntries);
                    data = tmpdata[0].Trim();
                    _comment = tmpdata[1];
                }
                if (data[data.Length - 1] == ',')
                    data = data.Remove(data.Length - 1);
                tabstr = data.Split(ch, StringSplitOptions.RemoveEmptyEntries);
                _name = tabstr[0];
                _datatype = tabstr[1];

                //int tabdevvalue = 2;
                //if (tabstr.Length >= 3)
                //{
                //    if (tabstr[tabdevvalue].Contains(')'))
                //    {
                //        _datatype += tabstr[tabdevvalue].Trim();
                //        tabdevvalue++;
                //    }
                //    _defaultvalue = tabstr[tabdevvalue];
                //    if (tabstr.Length >= tabdevvalue + 2 && tabstr[tabdevvalue + 1].IndexOf("OUTPUT", StringComparison.CurrentCultureIgnoreCase) >= 0)
                //    {
                //        _output = true;
                //    }
                //}
                int tabdevvalue = 2;
                if (tabstr.Length >= 3)
                {
                    if (tabstr[tabdevvalue].Contains(')'))
                    {
                        _datatype += tabstr[tabdevvalue].Trim();
                        tabdevvalue++;
                    }
                    if (tabstr.Length >= tabdevvalue + 1)
                    {
                        if (tabstr[tabdevvalue].IndexOf("OUTPUT", StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            _output = true;
                        }
                        else
                        {
                            _defaultvalue = tabstr[tabdevvalue];
                            if (tabstr.Length >= tabdevvalue + 2 && tabstr[tabdevvalue + 1].IndexOf("OUTPUT", StringComparison.CurrentCultureIgnoreCase) >= 0)
                            {
                                _output = true;
                            }
                        }
                    }
                }
            }
            else
            {
                char ch = ' ';
                if (data[data.Length - 1] == ',')
                    data = data.Remove(data.Length - 1);
                tabstr = data.Split(ch);

                _name = tabstr[0];
                _datatype = tabstr[1];
                //if (tabstr.Length >= 3 && tabstr[2].ToUpper() == "OUTPUT")
                //{
                //    _output = true;
                //}
                int tabdevvalue = 2;
                if (tabstr.Length >= 3)
                {
                    if (tabstr[tabdevvalue].Contains(')'))
                    {
                        _datatype += tabstr[tabdevvalue].Trim();
                        tabdevvalue++;
                    }
                    if (tabstr.Length >= tabdevvalue + 1)
                    {
                        if (tabstr[tabdevvalue].IndexOf("OUTPUT", StringComparison.CurrentCultureIgnoreCase) >= 0)
                        {
                            _output = true;
                        }
                        else
                        {
                            _defaultvalue = tabstr[tabdevvalue];
                            if (tabstr.Length >= tabdevvalue + 2 && tabstr[tabdevvalue + 1].IndexOf("OUTPUT", StringComparison.CurrentCultureIgnoreCase) >= 0)
                            {
                                _output = true;
                            }
                        }
                    }
                }
                //_defaultvalue = tabstr[2];
            }
        }

        public override string ToString()
        {
            return base.Name;
        }
    }
}
